package cn.stylefeng.guns.sys.modular.tao.service.serviceImpl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.tao.entity.Classification;
import cn.stylefeng.guns.sys.modular.tao.mapper.ClassificationMapper;
import cn.stylefeng.guns.sys.modular.tao.model.params.ClassificationParam;
import cn.stylefeng.guns.sys.modular.tao.model.result.ClassificationResult;
import  cn.stylefeng.guns.sys.modular.tao.service.ClassificationService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 分类管理表 服务实现类
 * </p>
 *
 * @author 
 * @since 2019-09-02
 */
@Service
public class ClassificationServiceImpl extends ServiceImpl<ClassificationMapper, Classification> implements ClassificationService {
    @Autowired
    ClassificationMapper ClassificationMapper;

    @Override
    public void add(ClassificationParam param){
        Classification entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(ClassificationParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(ClassificationParam param){
        Classification oldEntity = getOldEntity(param);
        Classification newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public ClassificationResult findBySpec(ClassificationParam param){
        return null;
    }

    @Override
    public List<ClassificationResult> findListBySpec(ClassificationParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(ClassificationParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public List<Map<String, Object>> selectMenuTree(String menuName, String level) {
        return ClassificationMapper.selectMenuTree(menuName, level);
    }

    @Override
    public Integer adds(ClassificationParam classificationParam) {
       return ClassificationMapper.adds(classificationParam);
    }

    @Override
    public Classification getByName(String name) {
        return ClassificationMapper.getByName(name);
    }

    @Override
    public List<Classification> getParentName() {
        return ClassificationMapper.getParentName();
    }

    @Override
    public List<Integer> getChildById(Integer id) {
        return ClassificationMapper.getChildById(id);
    }

    @Override
    public void deleteById(Integer id) {
        ClassificationMapper.deleteById(id);
    }

    @Override
    public List<Classification> getLevel2And() {
        return ClassificationMapper.getLevel2And();
    }

    @Override
    public List<Classification> getMenu() {
        return ClassificationMapper.getMenu();
    }

    @Override
    public List<Classification> getGoodsById(String id) {
        return ClassificationMapper.getGoodsById(id);
    }

    @Override
    public List<Classification> getGoodsType(ClassificationParam param) {
        return ClassificationMapper.getGoodsType(param);
    }

    private Serializable getKey(ClassificationParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Classification getOldEntity(ClassificationParam param) {
        return this.getById(getKey(param));
    }

    private Classification getEntity(ClassificationParam param) {
        Classification entity = new Classification();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
